import { produce } from 'immer'

export enum UserActionType {
  SET_USER = 'user/SET_USER',
  RESET_USER = 'user/RESET_USER'
}

export interface User {
  id: number
  userId: string
  username: string
  avatar: string
}

export type UserAction  = {
  type: UserActionType
  payload: object
}

export interface UserState {
  userInfo: User
}

const initUserState: UserState = {
  userInfo: {
    id: -1,
    userId: '',
    username: '',
    avatar: '',
  }
}

export default function userReducer(state: UserState = initUserState, action: UserAction) {
  return produce(state, draft => {
    switch(action.type) {
      case UserActionType.SET_USER: 
        draft.userInfo = {
          ...draft.userInfo,
          ...action.payload
        }
        break
      case UserActionType.RESET_USER: 
        draft.userInfo = initUserState.userInfo
        break
      default:
        break
    }
  })
  
}